package com.ruoyi.syf.service.impl;

import java.util.List;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.annotation.ConvertField;
import com.ruoyi.common.annotation.ConvertFieldItem;
import com.ruoyi.common.enums.ConvertType;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.syf.domain.RiskAnalysisUnit;
import com.ruoyi.syf.domain.vo.RiskAnalysisUnitVo;
import com.ruoyi.syf.mapper.RiskAnalysisUnitMapper;
import com.ruoyi.syf.service.IRiskAnalysisUnitService;
import com.ruoyi.syf.service.IRiskShutdownRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 安全分析单元Service业务层处理
 *
 * @author yr
 * @date 2024-07-04
 */
@Service
public class RiskAnalysisUnitServiceImpl extends ServiceImpl<RiskAnalysisUnitMapper,RiskAnalysisUnit> implements IRiskAnalysisUnitService {
    @Autowired
    private RiskAnalysisUnitMapper riskAnalysisUnitMapper;
    @Autowired
    private IRiskShutdownRecordService shutdownRecordService;

    /**
     * 查询安全分析单元
     *
     * @param id 安全分析单元主键
     * @return 安全分析单元
     */
    @Override
    public RiskAnalysisUnit selectRiskAnalysisUnitById(Long id) {
        return riskAnalysisUnitMapper.selectById(id);
    }

    /**
     * 查询安全分析单元列表
     *
     * @param riskAnalysisUnit 安全分析单元
     * @return 安全分析单元
     */
    @ConvertField({
            @ConvertFieldItem(fieldName = "deptId",assistFieldName = "deptName",type = ConvertType.DEPT),
            @ConvertFieldItem(fieldName = "userId",assistFieldName = "userName",type = ConvertType.USER)
    })
    @Override
    public List<RiskAnalysisUnitVo> list(RiskAnalysisUnitVo riskAnalysisUnit) {
        return riskAnalysisUnitMapper.list(riskAnalysisUnit);
    }

    /**
     * 新增安全分析单元
     *
     * @param riskAnalysisUnit 安全分析单元
     * @return 结果
     */
    @Override
    public int insertRiskAnalysisUnit(RiskAnalysisUnit riskAnalysisUnit) {
        return riskAnalysisUnitMapper.insert(riskAnalysisUnit);
    }

    /**
     * 修改安全分析单元
     *
     * @param riskAnalysisUnit 安全分析单元
     * @return 结果
     */
    @Override
    public int updateRiskAnalysisUnit(RiskAnalysisUnit riskAnalysisUnit) {
        return riskAnalysisUnitMapper.updateById(riskAnalysisUnit);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean upStatusList(Long objectId, Boolean status, String reason) {
        LambdaQueryWrapper<RiskAnalysisUnit> lqw = new LambdaQueryWrapper<RiskAnalysisUnit>()
                .eq(RiskAnalysisUnit::getObjectId, objectId)
                .eq(RiskAnalysisUnit::getStatus, status)
                .eq(RiskAnalysisUnit::getDelFlag, 0);
        List<RiskAnalysisUnit> units = riskAnalysisUnitMapper.selectList(lqw);
        if (CollectionUtils.isNotEmpty(units)) {
            units.forEach(unit -> unit.setStatus(!status));
            updateBatchById(units);
            List<Long> unitIds = units.stream().map(RiskAnalysisUnit::getId).collect(Collectors.toList());
            if (status){
                shutdownRecordService.saveList(objectId,unitIds,reason);
            }else {
                shutdownRecordService.updateList(objectId,unitIds);
            }
        }
        return true;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean upStatus(Long id, String reason) {
        RiskAnalysisUnit unit = baseMapper.selectById(id);

        //校验权限
        SecurityUtils.hasUser(unit.getUserId());

        Boolean status = unit.getStatus();
        unit.setStatus(!status);
        updateById(unit);
        if (status){
            shutdownRecordService.save(unit.getObjectId(),id,reason);
        }else {
            shutdownRecordService.update(unit.getObjectId(),id);
        }
        return true;
    }

    @Override
    public boolean upUnitScan(Long id) {
        RiskAnalysisUnit unit = baseMapper.selectById(id);

        //校验权限
        SecurityUtils.hasUser(unit.getUserId());

        unit.setScan(!unit.getScan());
        return updateById(unit);
    }

    /**
     * 批量删除安全分析单元
     *
     * @param ids 需要删除的安全分析单元主键
     * @return 结果
     */
    @Override
    public int deleteRiskAnalysisUnitByIds(Long[] ids) {
        return riskAnalysisUnitMapper.deleteRiskAnalysisUnitByIds(ids);
    }

    /**
     * 删除安全分析单元信息
     *
     * @param id 安全分析单元主键
     * @return 结果
     */
    @Override
    public int deleteRiskAnalysisUnitById(Long id) {
        return riskAnalysisUnitMapper.deleteRiskAnalysisUnitById(id);
    }

    @Override
    public void isScan(Long id) throws IllegalAccessException {
        RiskAnalysisUnit unit = getById(id);
        if ((unit.getScan() != null && unit.getScan())){
            throw new IllegalAccessException("该单元下的排查任务仅支持扫码排查！");
        }
    }
}
